﻿OB.Emes_ActionButton = OB.Emes_ActionButton || {};

//质量数据采集
OB.Emes_ActionButton.Quality_OrderTable_Collection = function(params, win) {
	var grid = win.view.viewGrid;
	var record = grid.getSelectedRecord();
	
	OB.Emes_ActionButton.orderOrderTableCollectionOpenWindow = win;
	OB.Emes_ActionButton.orderOrderTableCollectionWindow = isc.Emesq_Quality_OrderTable_CollectionWindow.create(record);
	OB.Emes_ActionButton.orderOrderTableCollectionWindow.show();
};
//质量判定
OB.Emes_ActionButton.Quality_Judgment_Collection = function(params, win) {
	var grid = win.view.viewGrid;
	var record = grid.getSelectedRecord();
	
	var data = {
		code: record.emesoManuorderJobOrder$_identifier
	};
	
	OB.Emes_ActionButton.orderOrderTableCollectionOpenWindow = win;
	OB.Emes_ActionButton.orderOrderTableCollectionWindow = isc.Emesq_Quality_OrderTable_CollectionWindow.create(data);
	OB.Emes_ActionButton.orderOrderTableCollectionWindow.show();
};
var Quality_CollectionView_Init_Data = {
	title: "质量数据采集",
	width: '80%',
	height: 400,
	align: 'center',
	defaultLayoutAlign: 'center',
	resultMap: {},
	reasonMap: {},
	allItem: [],
	orderId: null,
	submit: function(isForce){
		var _self = this;
		var orderCode = this.jobOrderCodeQtyForm.getValue("jobOrderCode");
		if(!orderCode){
			isc.showMessage("作业订单号不能为空", isc.OBMessageBar.TYPE_ERROR);
			return;
		}
		
		var code = this.dataCollectionInfo.getValue("code");
		var description = this.dataCollectionInfo.getValue("description");
		
		var form2Group = this.dataCollectionInfo.getField("group").itemIds;
		var form3Group = this.addCollectionItem.getField("group").itemIds;
		var arr = [];
		
		for(var i = 0 ; i < form2Group.length ; i ++){
			if(form2Group[i].indexOf("_item_") == 0 ){
				var item = this.dataCollectionInfo.getField(form2Group[i]);
				var obj = {};
				obj.dataType = item.dataType;
				obj.value = item.getValue();
				obj.id = item.paramId;
				obj.groupId = item.groupId;
				obj.seqNo = item.seqNo;
				arr.push(obj);
			}
		}
		
		for(var i = 0 ; i < form3Group.length ; i ++){
			if(form3Group[i].indexOf("_itemTextValue_") == 0 ){
				var itemValue = _self.addCollectionItem.getField(form3Group[i]);
				var obj = {};
				obj.dataType = itemValue.dataType;
				obj.value = itemValue.getValue();
				obj.id = itemValue.paramId;
				arr.push(obj);
			}
		}
		//校验数据采集项目
		for(var i = 0 ; i < arr.length ; i ++){
			if((arr[i].value == null || arr[i].value == "") && arr[i].dataType != 'SELECTOR'){
				isc.showMessage("采集选项值不能为空", isc.OBMessageBar.TYPE_ERROR);
				return;
			}
			if(arr[i].dataType == 'NUMBER' && !/^[0-9]+$/.test(arr[i].value)){
				isc.showMessage("采集选项为数值的请录入数字", isc.OBMessageBar.TYPE_ERROR);
				return;
			}
		}
		
		if (this.submitLoading) {
			return;
		}
		
		isc.confirm("确定提交质量数据？", function(bool){
			if (bool) {
				var requestData = {
					method: "submit",
					entityName: _self.entityName,
					orderCode: orderCode,
					code: code,
					description: description,
					arr: arr
				};
				_self.submitLoading = true;
				OB.RemoteCallManager.call("com.mes.handler.job.EmesqQualityCollectionHander", requestData, {},
					function(response, data, request) {
						_self.submitLoading = false;
						if(data.result){
							if (OB.Emes_ActionButton.orderOrderTableCollectionOpenWindow) {
								OB.Emes_ActionButton.orderOrderTableCollectionOpenWindow.refresh();
							}
							if (OB.Emes_ActionButton.orderOrderTableCollectionWindow) {
								OB.Emes_ActionButton.orderOrderTableCollectionWindow.closeClick();
							} else {
								_self.clearForm();
							}
							isc.showMessage("提交成功！", isc.OBMessageBar.TYPE_INFO);
						} else {
							isc.showMessage(data.text, isc.OBMessageBar.TYPE_ERROR);
						}
					}
				);
			}
		});
	},
	initWidget: function (data) {
		this.entityName = data._entityName;
		
		//OB控件 追加	addField or addItem	会有样式和事件找不到的问题
		this.createJobOrderForm(data);
		this.createForm();
		
		var pageLayout = isc.VLayout.create({
			width: '100%',
			height: 380,
			layoutMargin: 5,
			membersMargin: 5,
			members : [
				this.jobOrderInfoLayout,
				this.dataCollectionInfo,
				this.addCollectionItem,
				this.buttonLayout
			]
		});
		
		//却别弹框与直接菜单打开方式
		if (data && data.code) {
			this.items = [pageLayout];
		} else {
			this.addMembers(pageLayout);
		}
		this.Super("initWidget", arguments);
		
		if (data && data.code) {
			this.orderData = data;
			this.jobOrderForm.search();
		} else {
			this.clearForm();
		}
	},
	createJobOrderForm: function(data) {
		var _self = this;
		this.jobOrderCodeQtyForm = isc.DynamicForm.create({
			ID: "jobOrderCodeQtyForm",
			width: '100%',
			height: 20,
			numCols : 6,
			fields: [
				{width: '*',type: 'text',title: '作业订单编码',name: 'jobOrderCode' ,value: (data && data.code ? data.code : ""),startRow: true,
				keyDown: function(field, form, keyName){
					if(keyName == "Enter"){
						jobOrderForm.search();
						return false;
					}
					return true;
				}}
			]
		});
		this.jobOrderForm = isc.DynamicForm.create({
			ID: "jobOrderForm",
			width: '100%',
			numCols: 6,
			search: function(){
				var orderCode = jobOrderCodeQtyForm.getValue("jobOrderCode");
				if(!orderCode) {
					isc.showMessage("请输入订单编码",isc.OBMessageBar.TYPE_INFO);
					return;
				}
				var requestData = {
					method: "getData",
					orderCode: orderCode,
					entityName: _self.entityName
				};
				OB.RemoteCallManager.call("com.mes.handler.job.EmesqQualityCollectionHander", requestData, {}, function(response, data, request){
					_self.clearForm();
					if(data && data.result){
						//只读默认值
						if (data.orderInfo) {
							_self.setValue(data.orderInfo, jobOrderForm);
							_self.setJobOrderStatus(data.orderInfo.orderStatus);
							_self.routeNo = data.orderInfo.routeNo
						}
						_self.jobOrderCodeQtyForm.getField("jobOrderCode").setCanEdit(false);
						_self.orderId = orderCode;
						_self.dataCollectionInfo.show();
						_self.buttonLayout.show();
						
						if (data.list) {
							_self.appendCollectionForm(data.list);
						}
						_self.allItem = data.allItem;
					}else{
						isc.showMessage(data.text, isc.OBMessageBar.TYPE_INFO);
					}
				});
			},
			fields : [
				{width: '*',name:'group',type: 'OBSectionItem',defaultValue:'作业订单信息',sectionExpanded: true,colSpan: 6,itemIds: 
					['manuOrderCode', 'productCode', 'productName', 'routeName', 'qty', 'orderStatus', 'description']},
				{width: '*',type: 'text',title:'生产订单',	name: 'manuOrderCode',	canEdit:false, startRow: true, readOnlyDisplay: "static"},
				{width: '*',type: 'text',title:'物料编号',	name: 'productCode',	canEdit:false, startRow: false, readOnlyDisplay: "static"},
				{width: '*',type: 'text',title:'物料名称',	name: 'productName',	canEdit:false, startRow: false, readOnlyDisplay: "static"},
				{width: '*',type: 'text',title:'工序名称',	name: 'routeName',		canEdit:false, startRow: true, readOnlyDisplay: "static"},
				{width: '*',type: 'text',title:'质检数量',	name: 'qty',			canEdit:false, startRow: false, readOnlyDisplay: "static"},
				{width: '*',type: 'text',title:'状态',		name: 'orderStatus',	canEdit:false, startRow: false, readOnlyDisplay: "static"},
				{width: '*',type: 'text',title:'备注',		name: 'description',	canEdit:false, startRow: true, readOnlyDisplay: "static", colSpan: 6, width: '100%'}
			]
		});
		
		var processStandardBtn = isc.OBFormButton.create({
			ID:'processStandardBtn',
			title : '工艺标准',
			action : function() {
				OB.RemoteCallManager.call("com.mes.handler.ViewDrawingsOrderHandler",
						{ routeNo : _self.routeNo , id :_self.orderId },
						{},
						function(response, data, request) {
							if(data.flag){
								isc.EMES_Window_View.create({url: "DownFileServlet.html?path=" + encodeURIComponent(data.meg) + "&online=true&fileName=" + _self.routeNo + ".pdf"}).show();
							}else{
								top.isc.showMessage("该工序未上传工序标准。",top.isc.OBMessageBar.TYPE_SUCCESS);
							}
							
							
						});
			}
		});
		var productPictureBtn = isc.OBFormButton.create({
			ID:'productPictureBtn',
			title : '产品图纸',
			action : function() {
				OB.RemoteCallManager.call("com.mes.handler.ViewmaterialOrderHandler",
						{id :_self.orderId },
						{},
						function(response, data, request) {
							if(data.flag){
								isc.EMES_Window_View.create({url: "DownFileServlet.html?path=" + encodeURIComponent(data.meg) + "&online=true&fileName=" + _self.routeNo + ".pdf"}).show();
							}else{
								isc.showMessage(data.message);
							}
							
							
						});
			}
		});
		var jobInstructionsBtn = isc.OBFormButton.create({
			ID:'jobInstructionsBtn',
			title : '作业指导书',
			action : function() {
				OB.RemoteCallManager.call("com.mes.handler.ViewprocessOrderHandler",
						{ routeNo : _self.routeNo , id :_self.orderId },
						{},
						function(response, data, request) {
							if(data.flag){
								isc.EMES_Window_View.create({url: "DownFileServlet.html?path=" + encodeURIComponent(data.meg) + "&online=true&fileName=" + _self.routeNo + ".pdf"}).show();
							}else{
								isc.showMessage(data.message);
							}
							
							
						});
			}
		});
		this.jobOrderInfoLayout = isc.HLayout.create({
			width: '100%',
			height: 50,
			layoutMargin: 0,
			memberMargin: 5,
			members:[
			 	isc.VLayout.create({
					width: '75%',
					height: 50,
					layoutMargin: 0,
					memberMargin: 0,
					members:[
			 			this.jobOrderCodeQtyForm,
			 			this.jobOrderForm
					]
				}),
			 	isc.VLayout.create({
					width: '25%',
					height: 50,
					layoutMargin: 0,
					memberMargin: 0,
					members:[
						processStandardBtn,
						productPictureBtn,
						jobInstructionsBtn
					]
				})
			]
		});
	},
	createForm: function() {
		var _self = this;
		this.dataCollectionInfo = isc.DynamicForm.create({
			ID: "dataCollectionInfo",
			numCols : 8,
			itemIds: [],
			fields : [
				{type: 'OBSectionItem', name: 'group', defaultValue: '采集项目', sectionExpanded: true, itemIds: ['description'], colSpan: 8},,
				{width: '*', type: 'text', title: '编号', name: 'code', canEdit: true, startRow: true},
				{width: '*', type: 'textarea', title: '备注', name: 'description', canEdit: true, startRow: true, colSpan: 3, height: 40}
			]
		});
		this.addCollectionItem = isc.DynamicForm.create({
			ID: "addCollectionItem",
			numCols : 8,
			fields : [
				{type: 'OBSectionItem',name:'group',defaultValue:'追加采集项目',sectionExpanded: true,itemIds: ['btnAdd'],colSpan:8},
				{width: '70', type: 'button', title: '新增采集项', name: 'btnAdd', click: function(){
					_self.appendOtherItems({
						name: "kkk21",
						itemDatatype: "2",
						title: "选项"
					});
				}}
			]
		});
		this.submitOkBtn = isc.OBFormButton.create({
			ID:'submitOkBtn',
			title : '提交',
			action : function() {
				_self.submit(false);
			}
		});
		this.submitDelBtn = isc.OBFormButton.create({
			ID:'submitDelBtn',
			title : '取消',
			action : function() {
				_self.clearForm();
				OB.Emes_ActionButton.orderOrderTableCollectionWindow.closeClick();
			}
		});
		this.buttonLayout = isc.HLayout.create({
			width: '100%',
			layoutMargin: 15,
			memberMargin: 30,
			align: 'center',
			defaultLayoutAlign: 'center',
			members:[
		 		this.submitOkBtn,
				this.submitDelBtn
			]
		});
	},
	appendCollectionForm: function(data, readOnly){
		var group = this.dataCollectionInfo.getItem("group").itemIds;
		var startRow = false;
		for(var i = 0 ; i < data.length ; i ++){
			startRow = (i % 3) == 0;
			var name = "_item_" + this.getIndex();
			group.push(name);
			if(data[i].type == "NUMBER"){//数字
				this.dataCollectionInfo.addField({
					seqNo: data[i].seqNo, paramId: data[i].id, groupId: data[i].groupId, dataType: data[i].type, value: data[i].value, 
					type: 'integer', title: data[i].name + "-数值", name: name, width: '*', length: 5, startRow: startRow
				});
			}else if(data[i].type == "SELECTOR"){//选项
				this.dataCollectionInfo.addField({
					seqNo: data[i].seqNo, paramId: data[i].id, groupId: data[i].groupId, dataType: data[i].type, value: data[i].value, 
					type: 'select', title: data[i].name + "-选项", name: name, width: '*', startRow: startRow, valueMap: data[i].child
				});
			} else {//描述
				this.dataCollectionInfo.addField({
					seqNo: data[i].seqNo, paramId: data[i].id, groupId: data[i].groupId, dataType: data[i].type, value: data[i].value, 
					type: 'text', title: data[i].name + "-文本", name: name, width: '*', length: 200, startRow: startRow, required: false});
			}
			if(readOnly){
				var column = this.dataCollectionInfo.getField(name);
				column.canEdit = false;
				column.setReadOnlyDisplay("static");
			}
		}
	},
	clearCollectionForm: function(){
		var group = this.dataCollectionInfo.getItem("group").itemIds;
		for(var i = group.size()-1 ; i >= 0 ; i --){
			if(group[i].indexOf("_item_") == 0){
				this.dataCollectionInfo.removeItem(dataCollectionInfo.getItem(group[i]));
				group.remove(group[i]);
			}
		}
	},
	appendOtherItems: function(data){
		var _self = this;
		var group = this.addCollectionItem.getItem("group").itemIds;
		var index = this.getIndex();
		var itemName = "_item_" + index;
		var itemTextValueName = "_itemTextValue_" + index;
		var itemButtonName = "_itemBtnDel_" + index;
		group.push(itemTextValueName);
		group.push(itemButtonName);
		group.push(itemName);//追缴到3个倍数 位置不能修改
		
		//数据采集项目
		var item = {type: 'select', title: '采集项目', name: itemName, width: '*', startRow: true, valueMap: {}, addUnknownValues: false, changed: function(){
			OB.RemoteCallManager.call("com.mes.handler.job.EmesqQualityCollectionHander",
				{method: "getDCItem", id: this.getValue()},{},
				function(response, data, request) {
					var item = data.item;
					var position = 0;
					for(var i = 0 ; i < group.length ; i ++){
						if(itemName == group[i]){
							position = i;
							break;
						}
					}
					if(position == 0){
						// form 是个表格	 之前加了obsectionitem 和 数据选项下拉框	因此	选项 使用表格占位	3/6/9
						//数据选项可以删除 ，位置无法从表单里面或者ITEM来获取	所以通过组 组本来需要管理所有控件	默认加入了 btnAdd	所以 数据选项	在 group位置调整到 3 6 9 的倍数
						return;
					}
					_self.addCollectionItem.clearValue(itemTextValueName);
					_self.addCollectionItem.removeItem(position);
					if("NUMBER" == item.type){
						var itemNumberValue = {paramId: item.id, dataType: item.type, width: '*', type: 'integer',
							name: itemTextValueName, length: 5, title: '数值', position: position};
						_self.addCollectionItem.addField(itemNumberValue, position);
					} else if("SELECTOR" == item.type) {
						var itemChild = {paramId: item.id, dataType: item.type, type: 'select', title: "选项", 
							name: itemTextValueName, width: '*', startRow: false, valueMap: item.child, addUnknownValues: false, position: position};
						_self.addCollectionItem.addField(itemChild, position);
					} else {//默认都是文本框
						var itemTextValue = {paramId: item.id, dataType: item.type, width: '*', type: 'text', 
							name: itemTextValueName, length: 200, title: '文本', position: position};
						_self.addCollectionItem.addField(itemTextValue, position);
					}
				}
			);
		}};
		item.valueMap = this.allItem;
		var itemTextValue = {dataType: 3, width: '*', type: 'text',name: itemTextValueName, length: 200, title:'描述'};
		var itemButton = {
			name: itemButtonName,
			width: '50',
			type: 'button',
			title: '-',
			startRow: false,
			removeName: [itemName, itemTextValueName, itemButtonName],
			click: function(){
				//从组中移除 
				for(var i = 0 ; i <	this.removeName.length ; i ++){
					var name = this.removeName[i];
					_self.addCollectionItem.removeItem(_self.addCollectionItem.getItem(name));
					group.remove(name);
				}
			}
		}
		
		this.addCollectionItem.addField(item);
		this.addCollectionItem.addField(itemTextValue);
		this.addCollectionItem.addField(itemButton);
	},
	clearOtherItems: function(){
		var group = this.addCollectionItem.getItem("group").itemIds;
		for(var i = group.size()-1 ; i >= 0 ; i --){
			var startWith = group[i].indexOf("_item");
			if(startWith == 0){
				this.addCollectionItem.removeItem(this.addCollectionItem.getItem(group[i]));
				group.remove(group[i]);
			}
		}
	},
	clearForm: function(){
		this.orderId = null;
		
		this.dataCollectionInfo.hide();
		this.buttonLayout.hide();
		this.jobOrderCodeQtyForm.getField("jobOrderCode").setCanEdit(true);
		
	},
	setValue: function(data, form){
		var itemIds = form.getFields();
		for(var i = 0 ; i < itemIds.length; i ++){
			var column = itemIds[i];
			this.setFieldValue(form.getField(column.name), data[column.name]);
		}
	},
	setFieldValue: function(field, value) {
		if (field.readOnlyDisplay = "static") {
			field.setReadOnlyDisplay("");
			field.setValue(value);
			field.setReadOnlyDisplay("static");
		} else {
			field.setValue(value);
		}
	},
	itemIndex: 0,
	getIndex: function() {
		return this.itemIndex ++;
	},
	setJobOrderStatus: function(value) {
		var status = "";
		if(value == 'CREATE'){
			status = "创建";
		} else if(value == 'START'){
			status = "已开工";
		} else if(value == 'FINISHED'){
			status = "已完工";
		} else if(value == 'OK'){
			status = "已质检";
		} else if(value == 'STOP'){
			status = "已暂停";
		}
		this.setFieldValue(this.jobOrderForm.getField("orderStatus"), status);	
	}
};

isc.defineClass("Emesq_Quality_OrderTable_CollectionView", isc.OBBaseView).addProperties(Quality_CollectionView_Init_Data);
isc.defineClass("Emesq_Quality_OrderTable_CollectionWindow", isc.OBPopup).addProperties(Quality_CollectionView_Init_Data);
